<?php
require_once 'classdb.php';
include("class.phpmailer.php");
include("class.smtp.php");
class tin extends db
{
    function laylienhetructuyen()
    {
        return $this->get_by_thutu("lienhe");
    }

    function laytuyendung($id)
    {
        return $this->get_by_thutu("tuyendung", $id, 1, '', 'limit 0,5');
    }

    function laydichvu($id, $limit = '')
    {
        return $this->get_by_thutu("dichvu", $id, 1, '', $limit);
    }

    function laybaohanh($id, $limit = '')
    {
        return $this->get_by_thutu("muahangtuxa", $id, 1, '', $limit);
    }

    function laysanphamchitiet($id, $limit = '')
    {
        return $this->get_by_thutu("sanpham", $id, 1, '', $limit);
    }

    function laytintuc($id)
    {
        return $this->get_by_thutu("tintuc", $id, 1);
    }

    function layloaisanpham($id = -1)
    {
        return $this->get_by_thutu("sanpham_loai", $id, 1);
    }

    function layloailopxe($id)
    {
        return $this->get_by_thutu("sanpham_loailop", -1, 1);
    }

    function laysanphammoi($id = -1, $limit = '')
    {
        return $this->get_by_thutu("sanpham", $id, 1, "", $limit);
    }

    function laysanpham($top = -1, $noibat = -1, $banchay = -1, $limit = '')
    {
        return $this->get_by_thutu("sanpham", -1, 1, "and(top='$top' or '$top'=-1) and(noibat='$noibat' or '$noibat'=-1) and(banchay='$banchay' or '$banchay'=-1)", $limit);
    }

    function laysanphamtheodanhmuc($idsanpham)
    {
        return $this->get_by_thutu("tin", -1, 1, "and idloai='$idsanpham'", $limit);
    }

    function laysanphamphantrang($pageNum = 1, $pageSize = 9, &$totalRows, $tukhoa = '', $idloailop = -1, $idloai = -1)
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "sanpham", -1, 1, "and ten_vi like '%$tukhoa%' and (idloailop='$idloailop' or '$idloailop'=-1) and (idloai='$idloai' or '$idloai'=-1)", '');
    }

    function laytintucphantrang($pageNum = 1, $pageSize = 9, &$totalRows, $id = -1)
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "tintuc", $id, 1);
    }

    function laydichvuphantrang($pageNum = 1, $pageSize = 9, &$totalRows, $id = -1)
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "dichvu", $id, 1);
    }

    function laybaohanhphantrang($pageNum = 1, $pageSize = 9, &$totalRows, $id = -1)
    {
        return $this->get_phantrang_db($pageNum, $pageSize, $totalRows, "muahangtuxa", $id, 1);
    }

    function laythongtin($id)
    {
        return $this->get_by_thutu("thongtin", $id);
    }

    function laythongtinkhac($id)
    {
        return $this->get_by_thutu("thongtinkhac", $id);
    }

    function laylienketweb()
    {
        return $this->get_by_thutu("lienketweb", -1, 1);
    }

    function layquangcao($vitri)
    {
        if ($vitri == 4)
            return $this->get_by_thutu("quangcao", -1, 1, " and vitri='$vitri'", $option2 = 'LIMIT 0,2');
        else
            return $this->get_by_thutu("quangcao", -1, 1, " and vitri='$vitri'");
    }

    function laytable($id)
    {
        return $this->get_by_thutu("field", $id);
    }

    function laysanphamkhac($id)
    {
        $sql = "select * from sanpham where id<>'$id' order by rand() limit 0,6";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
    }

    function getNewProduct()
    {
        $sql = "select * from sanpham where 1 order by thutu desc limit 0,20";
        $kq = mysql_query($sql) or die(mysql_error());
        return $kq;
    }

    function TongTienGioHang()
    {
        $sosp = count($_SESSION['daySoLuong']);
        if ($sosp <= 0) return 0;
        reset($_SESSION['daySoLuong']);
        reset($_SESSION['dayDonGia']);
        $tongtien = 0;
        for ($i = 0; $i < $sosp; $i++) {
            $soluong = current($_SESSION['daySoLuong']);
            $dongia  = current($_SESSION['dayDonGia']);
            $tongtien += $soluong * $dongia;
            next($_SESSION['daySoLuong']);
            next($_SESSION['dayDonGia']);
        }
        //for
        return $tongtien;
    }

    function LuuDonHang(&$error)
    {
        //Tiếp nhận dữ liệu
        $hoten        = $_POST['hoten'];
        $diachi       = $_POST['diachi'];
        $dienthoai    = $_POST['dienthoai'];
        $ngaynhanhang = $_POST['ngaydangki'];
        $noidung      = $_POST['noidung'];

        //kiểm tra dữ liệu
        if (count($_SESSION['daySoLuong']) == 0) {
            $error[] = "Bạn chưa chọn sản phẩm nào";
            return false;
        }

        $hoten      = trim(strip_tags($hoten));
        $dienthoai  = trim(strip_tags($dienthoai));
        $ngaydangki = trim(strip_tags($ngaydangki));
        $noidung    = trim(strip_tags($noidung));

        if (count($error) > 0) return false;

        if (get_magic_quotes_gpc() == false) {
            $hoten      = mysql_real_escape_string($hoten);
            $dienthoai  = mysql_real_escape_string($dienthoai);
            $ngaydangki = mysql_real_escape_string($ngaydangki);
            $noidung    = mysql_real_escape_string($noidung);
        }
        $Ngay_arr = explode("/", $ngaydangki); // array(17,11,2010)
        if (count($Ngay_arr) == 3) {
            $d = $Ngay_arr[0]; //17
            $m = $Ngay_arr[1]; //11
            $y = $Ngay_arr[2]; //2010
            if (checkdate($m, $d, $y) == false) $ngaydangki = date("Y-m-d");
            else $ngaydangki = $y . "-" . $m . "-" . $d;
        } else $ngaydangki = date("Y-m-d");
        $ngay = date("Y-m-d h:i:s");

        //lưu dữ liệu vào db
        $sessid   = session_id();
        $tongtien = $this->TongTienGioHang();
        if (isset($_SESSION['id']) == false) {
            $sql = "INSERT INTO donhang SET
			tennguoinhan = '$hoten', diachi = '$diachi',
			dtnguoinhan = '$dienthoai', ghichu = '$noidung',
			thoidiemnhanhang = '$ngaydangki', sessionid = '$sessid', TongTien='$tongtien',ngay='$ngay' ";
            //echo $sql; die;
            $kq = mysql_query($sql) or die(mysql_error());
            $_SESSION['id'] = mysql_insert_id();
        } else {
            $idDH = $_SESSION['id'];
            $sql  = "UPDATE donhang SET
			 tennguoinhan = '$hoten', diachi = '$diachi',
			dtnguoinhan = '$dienthoai', ghichu = '$noidung',
			thoidiemnhanhang = '$ngaydangki', sessionid = '$sessid' , TongTien='$tongtien' , ngay='$ngay'
			WHERE id = $idDH";
            //echo $sql; die;
            $kq = mysql_query($sql) or die(mysql_error());
        }
    } //function LuuDonHang

    function LuuChiTietDonHang()
    {
        $sosp = count($_SESSION['daySoLuong']);
        if ($sosp <= 0) {
            echo "Không có sản phẩm";
            return;
        }
        if (isset($_SESSION['id']) == false) {
            echo "Không có idDH";
            return;
        }
        $idDH = $_SESSION['id'];
        $sql  = "DELETE FROM donhangchitiet WHERE iddh = $idDH";
        mysql_query($sql) or die(mysql_error());
        reset($_SESSION['daySoLuong']);
        reset($_SESSION['dayDonGia']);
        reset($_SESSION['dayTenSP']);
        reset($_SESSION['dayGiamGia']);
        for ($i = 0; $i < $sosp; $i++) {
            $idSP    = key($_SESSION['daySoLuong']);
            $tensp   = current($_SESSION['dayTenSP']);
            $soluong = current($_SESSION['daySoLuong']);
            $gia     = current($_SESSION['dayDonGia']);
            $giam    = current($_SESSION['dayGiamGia']);
            $sql     = "INSERT INTO donhangchitiet (iddh, idsp, tensp, soluong, gia)
				  VALUES ($idDH, $idSP, '$tensp',$soluong, $gia)";
            //echo $sql;die;
            mysql_query($sql) or die(mysql_error());
            next($_SESSION['daySoLuong']);
            next($_SESSION['dayDonGia']);
            next($_SESSION['dayTenSP']);
            next($_SESSION['dayGiamGia']);
        }
        //for
    }

    //function LuuChiTietDonHang

    function send_mail($fromname, $to, $body)
    {
        $mail = new PHPMailer();
        $mail->IsSMTP(); // set mailer to use SMTP
        $mail->Host       = "smtp.gmail.com"; // specify main and backup server
        $mail->Port       = 465; // set the port to use
        $mail->SMTPAuth   = true; // turn on SMTP authentication
        $mail->SMTPSecure = 'ssl';
        $mail->Username   = "anh.pham2988@gmail.com"; // your SMTP username or your gmail username dia
        $mail->Password   = "Trong@nh"; // your SMTP password or your gmail password
        $from             = ""; // Reply to this email
        //$to="lilyngoc.hoang9@gmail.com"; // Recipients email ID enquiry@vietnamtourism-jsc.com
        $name           = ""; // Recipient's name
        $mail->From     = $from;
        $mail->FromName = $fromname; // Name to indicate where the email came from when the recepient received
        $mail->AddAddress($to, $name);
        //$mail->AddReplyTo($from,"Vo Duy Tuan");
        $mail->WordWrap = 50; // set word wrap
        $mail->IsHTML(true); // send as HTML
        $mail->Subject = "Khach hang lien he";
        //echo $body; die;
        $mail->Body    = $body; //HTML Body
        $mail->AltBody = ""; //Text Body
        //$mail->SMTPDebug = 2;
        $mail->Send();
    }

    function LuuThongTin()
    {
        $ipAddress     = $_SERVER['REMOTE_ADDR'];
        $userAgent     = mysql_real_escape_string($_SERVER['HTTP_USER_AGENT']);
        $username      = $_SESSION['kt_login_user'];
        $lastVisit     = time();
        $session_start = time();
        $idSession     = session_id();
        //echo $idSession;die;
        $sql = "SELECT idSession FROM sessions WHERE idSession='$idSession'";
        //echo $sql;die;
        $ses = mysql_query($sql) or die (mysql_error());

        if (mysql_num_rows($ses) > 0) { // người này có rồi, giờ request lại
            $sql = "	UPDATE sessions SET
				lastVisit = $lastVisit
				WHERE idSession='$idSession'";
            mysql_query($sql) or die(mysql_error() . " : " . $sql);
        } else { //người này chưa có, mới vào lần đầu
            $sql = "INSERT INTO sessions SET
				idSession = '$idSession',	userAgent = '$userAgent',
				lastVisit = $lastVisit,	session_start = $session_start,
				ipAddress = '$ipAddress'";
            mysql_query($sql) or die(mysql_error());
        }

        $sessionTime = 30; //thời gian lưu thông tin
        $sql         = "DELETE FROM sessions WHERE $lastVisit - lastVisit >= $sessionTime * 60";
        //echo $sql;die;
        mysql_query($sql) or die(mysql_error());
    }

}

?>
